package alisms

import (
	"autoclave/global"
	"fmt"
	openapi "github.com/alibabacloud-go/darabonba-openapi/client"
	dysmsapi "github.com/alibabacloud-go/dysmsapi-20170525/v2/client"
	"github.com/alibabacloud-go/tea/tea"
)

type SmsParams struct {
	PhoneNumbers  *string
	SignName      *string
	TemplateCode  *string
	TemplateParam *string
}

var accessKeyId string
var accessKeySecret string
var signName string

func init() {
	accessKeyId = global.AppConfig.Section("aliyun").Key("accessKeyId").String()
	accessKeySecret = global.AppConfig.Section("aliyun").Key("accessKeySecret").String()
	signName = global.AppConfig.Section("aliyun").Key("signName").String()
}

func CreateSmsClient() (client *dysmsapi.Client, _err error) {
	Config := &openapi.Config{
		// 您的 AccessKey ID
		AccessKeyId: &accessKeyId,
		// 您的 AccessKey Secret
		AccessKeySecret: &accessKeySecret,
	}
	// 访问的域名
	Config.Endpoint = tea.String("dysmsapi.aliyuncs.com")
	client, _err = dysmsapi.NewClient(Config)
	return client, _err
}

func SendSmsAliyun(phoneNumbers, TemplateCode, TemplateParam string) (Code string, Message string, err error) {
	smsClient, err := CreateSmsClient()
	if err != nil {
		fmt.Println("CreateSmsClient error: ", err.Error())
		return
	}
	requestBody := dysmsapi.SendSmsRequest{
		PhoneNumbers:  &phoneNumbers,
		SignName:      &signName,
		TemplateCode:  &TemplateCode,
		TemplateParam: &TemplateParam,
	}
	res, err := smsClient.SendSms(&requestBody)
	if err != nil {
		return
	}
	Code = *res.Body.Code
	Message = *res.Body.Message
	return
}
